package com.lupus.common;

import java.util.*;

import org.apache.poi.hssf.usermodel.*;

public final class CommonFactory {
	public static Map<String,Object> buildAction(String action,String fieldNames,String tableName,String joinTables,String condition,String localLanguage){
		Map<String,Object> actionMap = new HashMap<String,Object>();
		actionMap.put("action",action);
		actionMap.put("fieldNames",fieldNames);
		actionMap.put("tableName",tableName);
		actionMap.put("joinTables",joinTables);
		actionMap.put("condition",condition);
		actionMap.put("localLanguage",localLanguage);
		return actionMap;
	}
	
	public static HSSFWorkbook exportExcel(List<Map> titleList,List<Map> dataList){
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet();
		HSSFRow row = sheet.createRow(0);
		Map<String,String> tempMap = null;
		HSSFCell cell = null;
		int cellIndex = 0;
		int titleLength = titleList.size();
		for(int i=0;i<titleLength;i++){
			tempMap = (Map<String,String>)titleList.get(i);
			cell = row.createCell(cellIndex++);
			cell.setCellType(HSSFCell.CELL_TYPE_STRING);
			cell.setCellValue(new HSSFRichTextString(tempMap.get("DISPLAYNAMETEXT")));
		}
		int dataLength = dataList.size();
		for(int i=0;i<dataLength;i++){
			tempMap = (Map<String,String>)dataList.get(i);
			row = sheet.createRow(i+1);
			cellIndex = 0;
			for(int j=0;j<titleLength;j++){
				Map<String,String> titleMap = (Map<String,String>)titleList.get(j);
				cell = row.createCell(j);
				cell.setCellType(HSSFCell.CELL_TYPE_STRING);
				String fieldName = titleMap.get("FIELDNAME");
				cell.setCellValue(new HSSFRichTextString(tempMap.get(fieldName)));
			}
		}
		return workbook;
	}
}
